#include "StdAfx.h"
#include "GraphicsFile.h"

using namespace Gdiplus;

//-----------------------------------------------------------------------------
GraphicsFile::GraphicsFile()
:	image			(NULL),
	data			(NULL),
	width			(0),
	height		(0),
	bit_depth	(0)
{

}

//-----------------------------------------------------------------------------
GraphicsFile::GraphicsFile(LPCWSTR filename)
:	image			(NULL),
	data			(NULL),
	width			(0),
	height		(0),
	bit_depth	(0)
{
	readFile(filename);
}

//-----------------------------------------------------------------------------
GraphicsFile::~GraphicsFile()
{
	delete image;
}

//-----------------------------------------------------------------------------
void GraphicsFile::readFile(LPCWSTR filename)
{
	delete image;

	image = new Bitmap(filename);

	Gdiplus::PixelFormat pf	= image->GetPixelFormat();
	width							= image->GetWidth();
	height						= image->GetHeight();
	bit_depth					= pf >> 8 & 0xFF;

	BitmapData bData;
	Rect rect(0,0,width,height);
	image->LockBits(&rect, ImageLockModeRead, pf, &bData);

	data = (BYTE*)bData.Scan0;
}

//-----------------------------------------------------------------------------
GraphicsFile &GraphicsFile::invertAlpha()
{
	for(int y = 0; y < height; y++)
	for(int x = 0; x < width; x++)
	{
		BYTE &a = data[y*4*width + 4*x + 3];
		a = 255 - a;
	}

	return *this;
}
